package com.example.controller;

import com.example.dto.EmpDto;
import com.example.pojo.Dept;
import com.example.pojo.Emp;
import com.example.pojo.MyResponseResult;
import com.example.pojo.PageBean;
import com.example.service.EmpService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public MyResponseResult<PageBean<Emp>> getByPage(@RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "10") Integer pageSize, EmpDto empDto) {
        // System.out.println("接收到的参数: page=" + page + ", pageSize=" + pageSize + ", empDto=" + empDto);
        PageBean<Emp> pageBean = empService.getByPage(page, pageSize, empDto);
        return MyResponseResult.ok(pageBean);
    }

    @DeleteMapping("/{ids}")
    public MyResponseResult<Object> deleteByIds(@PathVariable Integer[] ids) {
        int i = empService.deleteByIds(ids);
        return i >= 1 ? MyResponseResult.ok(null) : MyResponseResult.error("删除失败");
    }

    @PostMapping
    public MyResponseResult<Object> insertEmp(@RequestBody Emp emp) {
        int i = empService.insertEmp(emp);
        return i >= 1 ? MyResponseResult.ok(null) : MyResponseResult.error("新增失败");
    }

    @GetMapping("/{id}")
    public MyResponseResult<Object> selectById(@PathVariable Integer id) {
        Emp emp = empService.selectById(id);

        return emp != null ? MyResponseResult.ok(emp) : MyResponseResult.error("查询失败");
    }

    @PutMapping
    public MyResponseResult<Object> updateEmp(@RequestBody Emp emp) {
        int i = empService.updateEmp(emp);
        return i >= 1 ? MyResponseResult.ok("null") : MyResponseResult.error("修改失败");
    }

}
